package com.ekeyman.securecreditlib.domain;

import java.io.Serializable;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.ekeyman.securecreditlib.annotations.Encrypt;

@Entity
@Table(name = "CREDITCARDS")
public class CreditCard implements Serializable {
	private static final long serialVersionUID = 3903549640115127166L;
	private long id;
	
	@Encrypt
	private String creditCardNumber;
	
	private String cardType;
	private String nameOnCard;
	private String fourDigits;
	private String expirationDate;
	private long version;
	private Set<CardTransaction> cardTransactions;
	
	public void setId(long id) {
		this.id = id;
	}
	
	@Id @GeneratedValue
	public long getId() {
		return id;
	}


	public void setCreditCardNumber(String creditCardNumber) {
		this.creditCardNumber = creditCardNumber;
	}
	
	@Column(name="CREDITCARDNUMBER", nullable=false)
	public String getCreditCardNumber() {
		return creditCardNumber;
	}
	
	public void setCardType(String cardType) {
		this.cardType = cardType;
	}
	
	@Column(name="CARDTYPE", nullable=false)
	public String getCardType() {
		return cardType;
	}
	
	public void setNameOnCard(String nameOnCard) {
		this.nameOnCard = nameOnCard;
	}

	@Column(name="NAMEONCARD", nullable=false)
	public String getNameOnCard() {
		return nameOnCard;
	}
	
	public void setFourDigits(String fourDigits) {
		this.fourDigits = fourDigits;
	}

	@Column(name="FOURDIGITS", nullable=false)
	public String getFourDigits() {
		return fourDigits;
	}
	
	public void setExpirationDate(String expirationDate) {
		this.expirationDate = expirationDate;
	}

	@Column(name="EXPIRATIONDATE", nullable=false)
	public String getExpirationDate() {
		return expirationDate;
	}
	
	public void setVersion(long version) {
		this.version = version;
	}
	
	@Column(name="VERSION")
	public long getVersion() {
		return version;
	}
	
	public void setCardTransactions(Set<CardTransaction> cardTransactions) {
		this.cardTransactions = cardTransactions;
	}
	
	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy="creditCard")
	public Set<CardTransaction> getCardTransactions() {
		return cardTransactions;
	}

	@Transient
	@Override
	public boolean equals(Object o){
		boolean result = false;
		if (o instanceof CreditCard){
			CreditCard cc = (CreditCard)o;
			if(getCardType().equalsIgnoreCase(cc.getCardType()) &&
					getNameOnCard().equalsIgnoreCase(cc.getNameOnCard()) &&
					getFourDigits().equalsIgnoreCase(cc.getFourDigits())) {
				result = true;
			}
		}
		return result;
	}

	@Transient
	@Override
	public int hashCode(){
		return getCardType().hashCode() +
			getNameOnCard().hashCode() + 
			getFourDigits().hashCode();
	}
}
